package com.band.controller;

import com.band.entity.Emp;
import com.band.entity.EmpQueryParam;
import com.band.entity.PageBean;
import com.band.entity.Result;
import com.band.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RequestMapping("/emps")
@RestController
public class EmpController {
    @Autowired
    private EmpService empService;

    /**
     * 条件查询员工列表
     *
     * @param param
     * @return
     */
    @GetMapping
    public Result list(EmpQueryParam param) {
        log.info("查询参数：{}", param);
        PageBean pageBean = empService.list(param);
        return Result.success(pageBean);
    }

    /**
     * 删除员工
     *
     * @param ids
     * @return
     */
    @DeleteMapping
    public Result delete(@RequestParam List<Integer> ids) {
        log.info("删除员工：{}", ids);
        empService.delete(ids);
        return Result.success();
    }

    @PostMapping
    public Result insert(@RequestBody Emp emp) {
        log.info("新增员工：{}", emp);
        empService.insert(emp);
        return Result.success();
    }
    /**
     * 查询员工
     * @param id
     * @return
     */

    @GetMapping("/{id}")
    public Result select(@PathVariable Integer id) {
        log.info("查询员工：{}", id);
        Emp emp = empService.select(id);
        return Result.success(emp);
    }

    /**
     * 更新员工
     * @param emp
     * @return
     */
    @PutMapping
    public Result update(@RequestBody Emp emp){
        log.info("更新员工：{}", emp);
        empService.update(emp);
        return Result.success();
    }

    @GetMapping("/list")
    public Result list(){
        List<Emp> emp = empService.list();
        return Result.success(emp);
    }

}
